
#include "apt32f003.h"
/* UART FIFO Operation functions
 * 
 * 
 * */

int fifo_addin (uint8_t dat, uint32_t *idx, uint32_t *odx, uint8_t *fifoaddr, uint32_t fifosize)
{
    if (*odx){
        if (*idx != (*odx-1)){
            fifoaddr[*idx] = dat;
            *idx += 1;
            if (*idx == fifosize){
                *idx = 0;
            }
        }else{
            return -1;
        }
    }else{
        if (*idx != fifosize-1){
            fifoaddr[*idx] = dat;
            *idx += 1;
        }else{
            return -1;
        }
    }
    return 0;
}

int fifo_getout(uint8_t *dat, uint32_t *idx, uint32_t *odx, uint8_t *fifoaddr, uint32_t fifosize)
{
    if (*odx != *idx){
        *dat = fifoaddr[*odx];
        *odx += 1;
        if (*odx == fifosize){
            *odx = 0;
        }
    }else{
        return -1;
    }
    return 0;
}

int fifo_peek(uint8_t *dat, uint32_t *idx, uint32_t *odx, uint32_t *fifoaddr)
{
    if (*odx != *idx){
        *dat = fifoaddr[*odx];
    }else{
        return -1;
    }
    return 0;
}

int fifo_delete(uint32_t *idx, uint32_t *odx, uint32_t fifosize)
{
    if (*odx != *idx){
        *odx += 1;
        if (*odx == fifosize){
            *odx = 0;
        }
    }else{
        return -1;
    }
    return 0;
}